package icu.lookyousmileface.netty.simple.mylmpl;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.Serializable;

/**
 * @author shenj
 * @title: NettyClient
 * @projectName NettyPro
 * @date 2020/12/4 8:30
 */
public class NettyClient implements Serializable  {

    private static final String host = "127.0.0.1";
    private static final int port = 5566;

    public static void main(String[] args) throws InterruptedException {

        EventLoopGroup mainLoop = new NioEventLoopGroup();
        try{
        Bootstrap clientStartApplicationConfiguration = new Bootstrap();

        clientStartApplicationConfiguration.group(mainLoop)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ClientHandler());
                    }
                });

        System.out.println("Client is ready....");

        ChannelFuture channelFuture = clientStartApplicationConfiguration.connect(host, port).sync();

        channelFuture.channel().closeFuture().sync();
        }finally {
            mainLoop.shutdownGracefully();
        }
    }
}
